<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <link rel="stylesheet" type="text/css" href="<%=basePath%>ext/easyui/themes/black/easyui.css?t=34355">
    <link rel="stylesheet" type="text/css" href="<%=basePath%>ext/easyui/themes/icon.css">
    <link rel="stylesheet" type="text/css" href="<%=basePath%>ext/easyui/themes/color.css">
    <link rel="stylesheet" type="text/css" href="<%=basePath%>ext/farm/farm.css">
    <link rel="stylesheet" type="text/css" href="<%=basePath%>ext/seed/grid.css">
    <script type="text/javascript" src="<%=basePath%>ext/farm/utils.js"></script>
    <script type="text/javascript" src="<%=basePath%>ext/easyui/jquery.min.js"></script>
    <script type="text/javascript" src="<%=basePath%>ext/easyui/jquery.easyui.min.js"></script>
    <script type="text/javascript" src="<%=basePath%>ext/easyui/plugins/jquery.edatagrid.js"></script>
    <script type="text/javascript" src="<%=basePath%>ext/easyui/locale/easyui-lang-zh_CN.js"></script>   
</head>
<style type="text/css">
	body, html{
		width: 100%; 
		height: 100%; 
		background-image: url(<%=basePath%>/images/backgroundImg.png);
	}
</style>
<body>
<div id="controlBox">
    <span style="color: white">种子名称:</span>
    <input id="genderSearch" class="easyui-textbox" panelHeight="auto">
    <a href="#" class="easyui-linkbutton c1" iconCls="icon-search" onclick="doSearch()">查询</a>
    <a href="#" class="easyui-linkbutton c2" iconCls="icon-add" onclick="addSeed()">添加</a>
    <a href="#" class="easyui-linkbutton c3" iconCls="icon-edit" onclick="loadForm()">编辑</a>
    <!-- javascript:grid.edatagrid('cancelRow') -->
    <a href="#" class="easyui-linkbutton c4" iconCls="icon-remove" onclick="javascript:$('#editSeedData').dialog('close')">取消</a>
    <a href="#" class="easyui-linkbutton c5" iconCls="icon-cancel" onclick="javascript:grid.edatagrid('destroyRow')">删除</a>
</div>
<table id="grid" style="width: 100%;"></table>
<div id="msgBox"></div> 

<div id="editSeedData" class="easyui-window" title="编辑数据" data-options="iconCls:'icon-edit', closed: true"
	style="width: 800px; padding: 5px;">
	<form id="formAddSeed" novalidate>
		<table id="addSeedTable">
			<tr>
				<td class="wideTd">ID:</td>
				<td class="inputTd"><input name="id" value="0" readonly="readonly"/></td>
				<td class="narrowTd"></td>
				<td class="wideTd">种子ID:</td>
				<td class="inputTd"><input name="seedId" class="easyui-numberbox"
					data-options="
						required:true,
						validType:'length[1,5]',
						invalidMessage: '有效长度1-5'"/>
				</td>
				<td class="narrowTd"></td>
			</tr>
			<tr>
				<td class="wideTd">种子名称:</td>
				<td class="inputTd"><input name="seedName" class="easyui-textbox"
					data-options="
						required:true,
						validType:'length[1,50]',
						invalidMessage: '有效长度1-50'"/></td>
				<td class="narrowTd"></td>
				<td class="wideTd">X季作物:</td>
				<td class="inputTd"><input name="xSeasonCrops" class="easyui-numberbox" 
					data-options="
						required:true,
						validType:'length[0, 1]',
						invalidMessage: '请输入0~4'"/>
				</td>
				<td class="narrowTd"></td>
			</tr>
			<tr>
				<td class="wideTd">种子等级:</td>
				<td class="inputTd"><input name="seedGrade" class="easyui-numberbox"
					data-options="
							required:true,
							validType:'length[1,5]',
							invalidMessage: '有效长度1-5'"/>
				</td>
				<td class="narrowTd"></td>
				<td class="wideTd">种子类型:</td>
				<td class="inputTd"><input name="seedType" class="easyui-combobox" panelHeight="auto"
					data-options="editable:false,
                                    valueField:'code',
                                    textField:'caption',
                                    required:true,
                                    url:'<%=basePath%>/seedType/data'"/>
                </td>
				<td class="narrowTd"></td>
			</tr>
			<tr>
				<td class="wideTd">可获经验:</td>
				<td class="inputTd"><input name="experience" class="easyui-numberbox"
					data-options="
								required:true,
								validType:'length[1,5]',
								invalidMessage: '有效长度1-5'"/>
				</td>
				<td class="narrowTd"></td>
				<td class="wideTd">每季成熟所需时间:</td>
				<td class="inputTd"><input name="seasonTime" class="easyui-numberbox"
					data-options="
								required:true,
								validType:'length[1,5]',
								invalidMessage: '有效长度1-5'"/>
				</td>
				<td class="narrowTd">秒</td>
			</tr>
			<tr>
				<td class="wideTd">每季成熟可获收成:</td>
				<td class="inputTd"><input name="seasonHarvest" class="easyui-numberbox"
					data-options="
								required:true,
								validType:'length[1,5]',
								invalidMessage: '有效长度1-5'"/>
				</td>
				<td class="narrowTd">个果实</td>
				<td class="wideTd">种子采购价:</td>
				<td class="inputTd"><input name="seedPurchasePrice" class="easyui-numberbox"
					data-options="
								required:true,
								validType:'length[1,5]',
								invalidMessage: '有效长度1-5'"/>
				</td>
				<td class="narrowTd">个金币</td>
			</tr>
			<tr>
				<td class="wideTd">每个收获的果实单价:</td>
				<td class="inputTd"><input name="fruitUnitPrice" class="easyui-numberbox"
					data-options="
								required:true,
								validType:'length[1,5]',
								invalidMessage: '有效长度1-5'"/>
				</td>
				<td class="narrowTd"></td>
				<td class="wideTd">土地需求:</td>
				<td class="inputTd"><input name="landDemand" class="easyui-combobox" panelHeight="auto"
					data-options="editable:false,
                                    valueField:'code',
                                    textField:'caption',
                                    required:true,
                                    url:'<%=basePath%>/landDemand/data'"/>
                </td>
				<td class="narrowTd"></td>
			</tr>
			<tr>
				<td class="wideTd">每季成熟可获积分:</td>
				<td class="inputTd"><input name="seasonGetPoint" class="easyui-numberbox"
					data-options="
								required:true,
								validType:'length[1,5]',
								invalidMessage: '有效长度1-5'"/>
                </td>
				<td class="narrowTd"></td>
				<td class="wideTd">提示信息:</td>
				<td class="inputTd"><input name="info" class="easyui-textbox"
					data-options="
					required:true,
					validType:'length[1,500]',
					invalidMessage: '有效长度1-500'"/></td>
				<td class="narrowTd"></td>
			</tr>
		</table>
	</form>
	<div id="formSeedButtons" style="margin-top: 1%">
    	<a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-ok" style="margin-left: 42%" onclick="saveAddSeedForm()">保存</a>
    	<a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-cancel" onclick="javascript:$('#editSeedData').dialog('close')">取消</a>
	</div>
</div>
<!-- 成长阶段编辑 -->
<div id="editCropsGrow" class="easyui-window" title="编辑成长阶段定义" data-options="iconCls:'icon-edit', closed: true"
	style="width: 80%; height: 500px; padding: 5px;">
	<iframe id="editCropsGrowIframe" style="width: 100%; height: 100%" frameborder="0" scrolling="no">
	</iframe>
</div>
<script>
	
	//点击种子商店后，恢复页面原布局
	parent.document.getElementById("tools").src = "tools.jsp";
	/* window.parent.document.body.rows='60,*,50'; */

	// 获取seedType的所有类型
	var seedTypeObject;
	var landDemandObject;
	$(function(){
		// 点击种子商店后，回复页面原布局
    	var url = '<%=basePath%>/seedType/data';
    	getRemoteData(url, getSeedTypeObject);
    })
    function getSeedTypeObject(data){
		seedTypeObject = data;
	}
	$(function(){
    	var url = '<%=basePath%>/landDemand/data';
    	getRemoteData(url, getLandDemandObject);
    })
    function getLandDemandObject(data){
		landDemandObject = data;
	}
	
    var params = {
        id: '',
        mode: 'insert'
    };
    var high = window.innerHeight - 45;
    var grid;
    $(document).ready(function () {
        //配置表格
        grid = $('#grid').edatagrid({
            title: '种子清单',
            method: 'post',
            height: high,
            /* height: getHeight(),  */
            url: '<%=basePath%>/seed/getList',
            <%-- saveUrl: '<%=basePath%>/seed/save', --%>
            updateUrl: '<%=basePath%>/seed/save',
            destroyUrl: '<%=basePath%>/seed/delete',
            border: false,
            rownumbers: true,  // 设置为true，则显示带有行号的列
            remoteSort: true,  // 定义是否从服务器排序数据
            nowrap: false,  // 设置为true，则把数据显示在一行里。设置为true能提高加载性能
            singleSelect: true,  // 设置为true，则只允许选中一行
            fitColumns: true,  // 使列自动展开/折叠以适应数据网格（datagrid）的宽度
            pageSize: 5,
            pageList: [5, 10, 15, 20],
            
            pagination: true,  // 设置为true，则在数据网格（datagrid）底部显示分页工具
            striped: true,  // 设置为true，则把行条纹化。（即奇偶行使用不同背景色）
            autoSave: true,  // 自动保存
            idField: "id",
            columns:[[
	        	{field: 'id', title: "ID", width: 30, sortable: true, align: 'center', halign: 'center'},
	        	{field: 'seedId', title: "种子ID", width: 40, align: 'center', halign: 'center',
	        		editor: {
                        type: 'numberbox',
                        options: {
                        	required:true,
    						validType:'length[1,5]',
    						invalidMessage: '有效长度1-5'
                        }
                    }	
	        	},
	        	{field: 'seedName', title: "种子名称", width: 60, sortable: true, align: 'center', halign: 'center',
	        		editor: {
                        type: 'textbox',
                        options: {
                        	required:true,
    						validType:'length[1,50]',
    						invalidMessage: '有效长度1-50'
                        }
                    }	
	        	},
	        	{field: 'xSeasonCrops', title: "X季作物", width: 80, align: 'center', halign: 'center',
	        		editor: {
	        			type: 'numberbox',
	        			options: {
	        				required:true,
							validType:'length[0, 1]',
							invalidMessage: '请输入0~4'
	        			}
	        		},
	        		formatter: function(value, row){
	        			return value + '季作物';
	        		}	
	        	},
	        	{field: 'seedGrade', title: "种子等级", width: 80, align: 'center', halign: 'center',
	        		editor: {
	        			type: 'numberbox',
	        			options: {
	        				required:true,
							validType:'length[1,5]',
							invalidMessage: '有效长度1-5'
	        			}
	        		},
	        		formatter: function(value, row){
	        			return value + '级作物';
	        		}	
	        	},
	        	{field: 'seedType', title: "种子类型", width: 60, align: 'center', halign: 'center',
	        		sortable: true,
	        		editor: {
	        			type: 'combobox',
	        			options: {
	        				editable:false,
                            valueField:'code',
                            textField:'caption',
                            data: seedTypeObject,
                            required:true,
                            panelHeight: 'auto',
	        			}
	        		},
	        		formatter: function (value, row) {
	        			return seedTypeObject[row.seedType].caption;
                    }	
	        	},
	        	{field: 'experience', title: "可获经验", width: 60, align: 'center', halign: 'center',
	        		editor: {
	        			type: 'numberbox',
	        			options: {
	        				required:true,
	    					validType:'length[1,5]',
	    					invalidMessage: '有效长度1-5'
	        			}
	        		},
	        	},
	        	{field: 'seasonTime', title: "每季成熟所需时间", width: 80, align: 'center', halign: 'center',
	        		editor: {
	        			type: 'numberbox',
	        			options: {
	        				required:true,
							validType:'length[1,5]',
							invalidMessage: '有效长度1-5'
	        			}
	        		},
	        		formatter: function(value, row){
	        			return value + '秒';
	        		}	
	        	},
	        	{field: 'seasonHarvest', title: "每季成熟可获收成", width: 80, align: 'center', halign: 'center',
	        		editor: {
	        			type: 'numberbox',
	        			options: {
	        				required:true,
							validType:'length[1,5]',
							invalidMessage: '有效长度1-5'
	        			}
	        		},	
	        	},
	        	{field: 'seedPurchasePrice', title: "种子采购价", width: 80, align: 'center', halign: 'center',
	        		editor: {
	        			type: 'numberbox',
	        			options: {
	        				required:true,
							validType:'length[1,5]',
							invalidMessage: '有效长度1-5'
	        			}
	        		},
	        		formatter: function(value, row){
	        			return value + '金币';
	        		}	
	        	},
	        	{field: 'fruitUnitPrice', title: "每个收获的果实单价", width: 110, align: 'center', halign: 'center',
	        		editor: {
	        			type: 'numberbox',
	        			options: {
	        				required:true,
							validType:'length[1,5]',
							invalidMessage: '有效长度1-5'
	        			}
	        		},
	        		formatter: function(value, row){
	        			return value + '金币';
	        		}
	        	},
	        	{field: 'landDemand', title: "土地需求", width: 60, align: 'center', halign: 'center',
	        		sortable: true,
	        		editor: {
	        			type: 'combobox',
	        			options: {
	        				editable:false,
                            valueField:'code',
                            textField:'caption',
                            data: landDemandObject,
                            required:true,
                            panelHeight: 'auto',
	        			}
	        		},
	        		formatter: function (value, row) {
	        			return landDemandObject[row.landDemand].caption;
                    }	
	        	},
	        	{field: 'seasonGetPoint', title: "每集成熟可获积分", width: 100, align: 'center', halign: 'center',
	        		editor: {
	        			type: 'textbox',
	        			options: {
	        				required:true,
							validType:'length[1,5]',
							invalidMessage: '有效长度1-5'
	        			}
	        		},
	        		formatter: function(value, row){
	        			return value + '分';
	        		}
	        	},
	        	{field: 'info', title: "提示信息", width: 100, sortable: true, align: 'center', halign: 'center',
	        		editor: {
	        			type: 'textbox',
	        			options: {
	        				required:true,
	    					validType:'length[1,500]',
	    					invalidMessage: '有效长度1-500'
	        			}
	        		},	
	        	},
	        	{field: 'cropStatus', title: "操作", width: 80, align: 'center', halign: 'center',
	        		formatter: function (value, row) {
                    	return '<button style="background-color: #0A80CF" id="' + row.seedId + '" class="operate" onclick="showEditCropsGrowData(' + row.seedId + ')">成长阶段</button>';
                    }	
	        	},
	        ]],
            destroyMsg: {
                norecord: {
                    title: '警告',
                    msg: '首先需要选中记录，然后在点击删除按钮'
                },
                confirm: {
                    title: '确认',
                    msg: '是否删除选中记录?'
                }
            },
            onSuccess: function (index, result) {
            	console.log(result);
                $("#msgBox").text(result.msg);
            },
            onDestroy: function (index, result) {
                console.log(result);
                $("#msgBox").text(result.msg);
            }
        });
        /* grid.datagrid("getPager").pagination({
        	pageSize: 5,
            pageList: [5, 10, 15, 20]
        }); */
    });
    
    function doSearch() {
        grid.datagrid("load", {
        	seedName: $("#genderSearch").val()
        });
    }
    
    function showEditCropsGrowData(seedId){
    	/* alert("this.id = " + value); */
    	var str = "<%=basePath%>cropsGrow/grid?seedId=" + seedId;
    	var editCropsGrowIframe = document.getElementById("editCropsGrowIframe");
    	editCropsGrowIframe.src = str;
    	$('#editCropsGrow').dialog('open');
    	
    }
    
    // 编辑数据
    function loadForm() {
        var row = grid.datagrid('getSelected');
       	console.log(row);
        if (row) {
            params.id = row.id;
            params.mode = 'edit';
            $('#editSeedData').dialog('open').dialog('setTitle', '编辑数据');
            $('#editSeedData').dialog('open').dialog('open');
            $('#formAddSeed').form('load', row);
        } else {
            alert("请先选择一行数据，然后再尝试点击操作按钮！");
        }
    }
    
 	// 添加
 	function addSeed(){
 		$("#grid").datagrid('clearSelections');
 		$('#editSeedData').dialog('open');
 		$('#formAddSeed').form('clear');
 		$('#formAddSeed').find("input[name='id']").val(0);
 	}
    
    // 保存
    function saveAddSeedForm() {
    	$('#formAddSeed').form('submit', {
            url: '<%=basePath%>/seed/save',
            onSubmit: function (param) {
                return $(this).form('validate');
            },
            success: function (result) {
            	console.log(result);
                var result = eval('(' + result + ')');  // eval本质就是执行一段代码。如果result是一个文本，那就可以通过这种方式转换为对象
                console.log(result);
                if (result.code == 0) {
                    $('#editSeedData').dialog('close');
                    grid.datagrid('reload');
                }
                $.messager.show({
                    title: "消息",
                    msg: result.msg
                });
            }
        });
    }
    
    window.addEventListener("resize", function(){
    	var h = window.innerHeight - 45;
    	$('#grid').edatagrid({
    		height: h,
    	})
    });
   
    
</script>
</body>
</html>





